/**
 * 响应拦截器
 */

import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from "@nestjs/common"
import { Observable } from 'rxjs'
import { map } from 'rxjs/operators'

@Injectable()
export class Response<T> implements NestInterceptor {
    intercept(context: ExecutionContext, next: CallHandler): Observable<{ data: T }> {
        return next.handle().pipe(map(data => {
            if (data.code) {
                return {
                    data: data.data,
                    code: data.code,
                    tips: data.tips
                }
            }
            return {
                data,
                code: '1000',
                tips: 'success'
            }
        }))
    }
}